package com.ciwong.epaper.modules.me.ui;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.bean.Price;
import com.ciwong.epaper.modules.me.bean.Service;
import com.ciwong.epaper.modules.me.bean.Trial;

import java.util.List;

/**
 * Created by young on 2017/12/27.
 */

public class NewBuyServicePriceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Price> priceList;
    private Service service;
    private Trial trial = null;
    private Price price;
    private Price curPrice = null;
    private boolean isTrial = false;
    private boolean isfree = false;
    private OnPriceClick onPriceClick;
    private Context context;
    private int curSelectPosition = 0;

    public void setOnPriceClick(OnPriceClick onPriceClick) {
        this.onPriceClick = onPriceClick;

    }

    public NewBuyServicePriceAdapter(Context context, List<Price> priceList, Service service, Trial trial) {
        this.context = context;
        this.priceList = priceList;
        this.service = service;
        this.trial = trial;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        try {
            if (null != trial && trial.getTrlalcount() == 0 && trial.getTrlalsum() > 0 && service.getSign() != 3 && viewType == 0) {
                //显示免费体验按钮
                view = View.inflate(parent.getContext(), R.layout.item_price_mouth, null);
                isTrial = true;
                return new NewBuyServicePriceAdapter.PriceMouthViewHolder(view);
            } else if (service.getSign() == 3 && viewType == 0) {//免费获取
                //隐藏免费体验按钮
                view = View.inflate(parent.getContext(), R.layout.item_service_price_free, null);
                isfree = true;
                return new NewBuyServicePriceAdapter.PriceFreeViewHolder(view);
            } else if (service.getSign() == 0) {
                //按月购买
                view = View.inflate(parent.getContext(), R.layout.item_price_mouth, null);
                return new NewBuyServicePriceAdapter.PriceMouthViewHolder(view);
            } else {
                //买断
                view = View.inflate(parent.getContext(), R.layout.item_service_price_free, null);
                return new NewBuyServicePriceAdapter.PriceFreeViewHolder(view);
            }

        } catch (Exception e) {
            e.getStackTrace();
        }
        return null;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        try {
            java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00");
            if (null != trial && trial.getTrlalcount() == 0 && trial.getTrlalsum() > 0 && service.getSign() != 3 && position == 0) {
                //显示免费体验按钮
                final NewBuyServicePriceAdapter.PriceMouthViewHolder priceMouthViewHolderfree = (NewBuyServicePriceAdapter.PriceMouthViewHolder) holder;
                int mTrialDay = trial.getTrlalsum();
                priceMouthViewHolderfree.price_tv.setText("免费体验");
                priceMouthViewHolderfree.price_date_view.setText(mTrialDay + "");
                if (curSelectPosition == position) {
                    priceMouthViewHolderfree.price_mouth_view.setBackground(context.getResources().getDrawable(R.mipmap.price_mouth_checked));
                    priceMouthViewHolderfree.price_date_view.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                    priceMouthViewHolderfree.price_data_type_view.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                    priceMouthViewHolderfree.price_tv.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                } else {
                    priceMouthViewHolderfree.price_mouth_view.setBackground(context.getResources().getDrawable(R.mipmap.price_mouth_unchecked));
                    priceMouthViewHolderfree.price_date_view.setTextColor(context.getResources().getColor(R.color.color_light_green));
                    priceMouthViewHolderfree.price_data_type_view.setTextColor(context.getResources().getColor(R.color.color_light_green));
                    priceMouthViewHolderfree.price_tv.setTextColor(context.getResources().getColor(R.color.color_light_green));
                }

                priceMouthViewHolderfree.price_mouth_view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onPriceClick.OnPriceClick(null, trial);
                        priceMouthViewHolderfree.price_mouth_view.setBackground(context.getResources().getDrawable(R.mipmap.price_mouth_checked));
                        priceMouthViewHolderfree.price_date_view.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                        priceMouthViewHolderfree.price_data_type_view.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                        priceMouthViewHolderfree.price_tv.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                        curPrice = null;
                        // notifyItemChanged(curSelectPosition);
                        //notifyItemChanged(position);
                        curSelectPosition = position;
                        notifyDataSetChanged();
                    }
                });
            } else if (service.getSign() == 3 && position == 0) {
                //免费获取
                //隐藏免费体验按钮
                final NewBuyServicePriceAdapter.PriceFreeViewHolder priceFreeViewHolderfree = (NewBuyServicePriceAdapter.PriceFreeViewHolder) holder;
                priceFreeViewHolderfree.free_price_view.setText("免费获取");
                priceFreeViewHolderfree.free_price_view.setBackground(context.getResources().getDrawable(R.mipmap.price_free));
                priceFreeViewHolderfree.free_price_view.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                priceFreeViewHolderfree.free_price_view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onPriceClick.OnPriceClick(null, null);
                        priceFreeViewHolderfree.free_price_view.setBackground(context.getResources().getDrawable(R.mipmap.price_free));
                        priceFreeViewHolderfree.free_price_view.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                    }
                });
            } else if (service.getSign() == 0) {
                //按月购买
                final NewBuyServicePriceAdapter.PriceMouthViewHolder priceMouthViewHolder = (NewBuyServicePriceAdapter.PriceMouthViewHolder) holder;
                if (!isfree) {
                    if (isTrial) {
                        price = priceList.get(position - 1);
                    } else {
                        price = priceList.get(position);
                    }
                    String tips = price.getTips();
                    float xjp = price.getPrice();
                    float oldp = price.getOldprice();
                    priceMouthViewHolder.price_date_view.setText(price.getUnit() + "");
                    priceMouthViewHolder.price_data_type_view.setText("个月");
                    String p = "￥" + df.format(xjp);
                    SpannableString spannableString = new SpannableString(p);
                    spannableString.setSpan(new TextAppearanceSpan(null, 0, 30, null, null), 0, 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    priceMouthViewHolder.price_tv.setText(spannableString);
                    if (oldp > 0) {
                        String old_p = "￥" + df.format(oldp);
                        SpannableString old_spannableString = new SpannableString(old_p);
                        old_spannableString.setSpan(new TextAppearanceSpan(null, 0, 30, null, null), 0, 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                        priceMouthViewHolder.old_price_tv.setText(old_spannableString);
                        priceMouthViewHolder.old_price_tv.setVisibility(View.VISIBLE);
                        priceMouthViewHolder.old_price_tv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
                    } else {
                        priceMouthViewHolder.old_price_tv.setVisibility(View.GONE);
                    }

                    if (tips != null && tips != "") {
                        priceMouthViewHolder.price_tips_tv.setText(tips);
                        priceMouthViewHolder.price_tips_tv.setVisibility(View.VISIBLE);
                    } else {
                        priceMouthViewHolder.price_tips_tv.setVisibility(View.GONE);
                    }
                    if (curSelectPosition == position) {
                        if (!isTrial) {//没有选中的情况下
                            priceMouthViewHolder.price_mouth_view.setBackground(context.getResources().getDrawable(R.mipmap.price_mouth_checked));
                            priceMouthViewHolder.price_date_view.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                            priceMouthViewHolder.price_tv.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                            priceMouthViewHolder.old_price_tv.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                            priceMouthViewHolder.price_tips_tv.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                            priceMouthViewHolder.price_data_type_view.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                        } else {
                            priceMouthViewHolder.price_mouth_view.setBackground(context.getResources().getDrawable(R.mipmap.price_mouth_checked));
                            priceMouthViewHolder.price_date_view.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                            priceMouthViewHolder.price_tv.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                            priceMouthViewHolder.old_price_tv.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                            priceMouthViewHolder.price_tips_tv.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                            priceMouthViewHolder.price_data_type_view.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                        }

                    } else {
                        priceMouthViewHolder.price_mouth_view.setBackground(context.getResources().getDrawable(R.mipmap.price_mouth_unchecked));
                        priceMouthViewHolder.price_date_view.setTextColor(context.getResources().getColor(R.color.color_light_green));
                        priceMouthViewHolder.price_tv.setTextColor(context.getResources().getColor(R.color.color_light_green));
                        priceMouthViewHolder.old_price_tv.setTextColor(context.getResources().getColor(R.color.color_light_green));
                        priceMouthViewHolder.price_tips_tv.setTextColor(context.getResources().getColor(R.color.color_light_green));
                        priceMouthViewHolder.price_data_type_view.setTextColor(context.getResources().getColor(R.color.color_light_green));
                    }
                    priceMouthViewHolder.price_mouth_view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (!isfree) {
                                if (isTrial) {
                                    price = priceList.get(position - 1);
                                } else {
                                    price = priceList.get(position);
                                }
                            }
                            onPriceClick.OnPriceClick(price, null);
                            priceMouthViewHolder.price_mouth_view.setBackground(context.getResources().getDrawable(R.mipmap.price_mouth_checked));
                            priceMouthViewHolder.price_date_view.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                            priceMouthViewHolder.price_tv.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                            priceMouthViewHolder.old_price_tv.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                            priceMouthViewHolder.price_tips_tv.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                            priceMouthViewHolder.price_data_type_view.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                            curPrice = price;
                            //notifyItemChanged(curSelectPosition);
                            //notifyItemChanged(position);
                            curSelectPosition = position;
                            notifyDataSetChanged();

                        }
                    });
                }
            } else {
                //买断
                final NewBuyServicePriceAdapter.PriceFreeViewHolder priceFreeViewHolder = (NewBuyServicePriceAdapter.PriceFreeViewHolder) holder;
                if (!isfree) {
                    if (isTrial) {
                        price = priceList.get(position - 1);
                    } else {
                        price = priceList.get(position);
                    }
                    String p = "￥" + df.format(price.getPrice());
                    SpannableString spannableString = new SpannableString(p);
                    spannableString.setSpan(new TextAppearanceSpan(null, 0, 30, null, null), 0, 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    priceFreeViewHolder.free_price_view.setText(spannableString);
                    if (curSelectPosition == position) {
                        if (!isTrial) {//没有选中的情况下
                            priceFreeViewHolder.free_price_view.setBackground(context.getResources().getDrawable(R.mipmap.price_free));
                            priceFreeViewHolder.free_price_view.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                        } else {
                            priceFreeViewHolder.free_price_view.setBackground(context.getResources().getDrawable(R.mipmap.price_free));
                            priceFreeViewHolder.free_price_view.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                        }

                    } else {
                        priceFreeViewHolder.free_price_view.setBackground(context.getResources().getDrawable(R.mipmap.price_foverer_unchecked));
                        priceFreeViewHolder.free_price_view.setTextColor(context.getResources().getColor(R.color.color_light_green));
                    }


                    priceFreeViewHolder.free_price_view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {

                            if (!isfree) {
                                if (isTrial) {
                                    price = priceList.get(position - 1);
                                } else {
                                    price = priceList.get(position);
                                }
                            }
                            onPriceClick.OnPriceClick(price, null);
                            priceFreeViewHolder.free_price_view.setBackground(context.getResources().getDrawable(R.mipmap.price_free));
                            priceFreeViewHolder.free_price_view.setTextColor(context.getResources().getColor(R.color.word_price_checked));
                            curPrice = price;
                            //notifyItemChanged(curSelectPosition);
                            //notifyItemChanged(position);
                            curSelectPosition = position;
                            notifyDataSetChanged();
                        }
                    });
                }
            }
        } catch (Exception e) {
            e.getStackTrace();
        }

    }

    @Override
    public int getItemCount() {
        try {
            if (service.getSign() == 3) {
                return 1;
            } else if (null != trial && trial.getTrlalcount() == 0 && trial.getTrlalsum() > 0 && service.getSign() != 3) {
                return priceList.size() + 1;
            } else {
                return priceList.size();
            }
        } catch (Exception e) {
            e.getStackTrace();
        }
        return 0;
    }


    @Override
    public int getItemViewType(int position) {
        return position;
    }

    static class PriceFreeViewHolder extends RecyclerView.ViewHolder {
        private TextView free_price_view;

        public PriceFreeViewHolder(View itemView) {
            super(itemView);
            free_price_view = (TextView) itemView.findViewById(R.id.free_price_view);
        }


    }

    static class PriceMouthViewHolder extends RecyclerView.ViewHolder {
        private TextView price_date_view, price_data_type_view, old_price_tv, price_tv, price_tips_tv;
        private ViewGroup price_mouth_view;

        public PriceMouthViewHolder(View itemView) {
            super(itemView);
            price_date_view = (TextView) itemView.findViewById(R.id.price_date_view);
            price_data_type_view = (TextView) itemView.findViewById(R.id.price_data_type_view);
            price_tv = (TextView) itemView.findViewById(R.id.price_tv);
            old_price_tv = (TextView) itemView.findViewById(R.id.old_price_tv);
            price_tips_tv = (TextView) itemView.findViewById(R.id.price_tips_tv);
            price_mouth_view = (ViewGroup) itemView.findViewById(R.id.price_mouth_view);
        }
    }

    public interface OnPriceClick {
        public void OnPriceClick(Price price, Trial trial);
    }
}








